package logic

import (
	"cart_srv/cart"
	"cart_srv/internal/svc"
	"cart_srv/model/mysql"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
)

type AddCartLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCartLogic {
	return &AddCartLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddCartLogic) AddCart(in *cart.AddCartRequest) (*cart.AddCartResponse, error) {
	// todo: add your logic here and delete this line
	c := mysql.Cart{
		UserID:     uint(in.UserId),
		GoodsID:    in.GoodsId,
		GoodsTitle: in.GoodsTitle,
		GoodsPrice: in.GoodsPrice,
		GoodsNum:   in.Num,
		Checked:    1,
	}
	err := c.Create()
	if err != nil {
		logx.Error("购物车添加失败", err)
		return nil, err
	}
	return &cart.AddCartResponse{}, nil
}
